package cn.erp.app.depot.controller;

import java.util.ArrayList;
import java.util.List;

import cn.erp.app.service.BaseInfoService;
import cn.erp.app.util.EuiRender;
import cn.erp.common.model.SvcBaseinfo;
import cn.erp.common.model.SvcMaterielSubdivision;
import cn.erp.common.model.SvcRecord;

import com.jfinal.common.controller.BaseController;
import com.jfinal.common.util.DateTimeFormat;
import com.jfinal.ext.ret.ReqResult;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

@ControllerBind(controllerKey = "/depot/inorout", viewPath = "/depot/inorout")
public class InorOutController extends BaseController {
	public void index() {
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMateriel();
		setAttr("materiel", baseInfo);
		List<SvcBaseinfo> wareHouse = BaseInfoService.me.getAllWareHouse();
		setAttr("warehouse",wareHouse);
		render("inorout_index.html");
	}

	public void getdata() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("rows", 10);

		SvcRecord svc = getModel(SvcRecord.class, "inorout");

		Page result = page(svc, pageNumber, pageSize);

		render(EuiRender.page(result));
	}

	public void saveorupdate() {
		SvcRecord svcProduce = getModel(SvcRecord.class, "inorout");
		svcProduce.setTotal(svcProduce.getNum()*svcProduce.getPrice());
		try {
			if (svcProduce.getId() != null && svcProduce.getId() != 0) {
				svcProduce.update();
				renderJson(ReqResult.UPDATE_SUCCESS);
			} else {
				svcProduce.setOperDate(DateTimeFormat.getDateTime());
				svcProduce.save();
				renderJson(ReqResult.ADD_SUCCESS);
			}

			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.ADD_ERROR);
	}

	public void getdetailtype() {
		String paramId = getPara("id");
		SvcBaseinfo svc = BaseInfoService.me.getMaterielByParamId(paramId);
		String table = svc.getMaptable();
		List<Record> item = new ArrayList<Record>();
		if (table.equals("svc_produce_category")) {
			item = Db
					.find("select id, produceName text from svc_produce_category");
		} else if (table.equals("svc_materiel_subdivision")) {
			item = Db
					.find("select id, MaterielName text from svc_materiel_subdivision where categoryId=?",svc.getParamId());

		}
		renderJson(item);
	}

	public void delete() {

		String[] ids = getParaValues("ids");
		try {
			if (ids != null && !ids.equals("")) {
				for (String id : ids) {
					SvcRecord.dao.deleteById(id);
				}
			}
			renderJson(ReqResult.DEL_SUCCESS);
			return;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		renderJson(ReqResult.DEL_ERROR);
	}

	public void openadd() {
		// long id = getParaToLong("id", 0L);
		SvcRecord pojo = new SvcRecord();
		// if (id != 0L) {
		// pojo = SvcRecord.dao.findById(id);
		//
		// }
		List<SvcBaseinfo> wareHouse = BaseInfoService.me.getAllWareHouse();
		setAttr("warehouse",wareHouse);
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMateriel();
		setAttr("materiel", baseInfo);
		setAttr("inorout", pojo);
		render("inorout_add.html");
	}

	public void openedit() {
		long id = getParaToLong("id", 0L);
		SvcRecord pojo = new SvcRecord();
		if (id != 0L) {
			pojo = SvcRecord.dao.findById(id);

		}
		List<SvcBaseinfo> wareHouse = BaseInfoService.me.getAllWareHouse();
		setAttr("warehouse",wareHouse);
		List<SvcBaseinfo> baseInfo = BaseInfoService.me.getAllMateriel();
		setAttr("materiel", baseInfo);
		setAttr("inorout", pojo);
		render("inorout_edit.html");
	}

}
